package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.R;
import com.reggie.dto.SetmealDto;
import com.reggie.entity.Category;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.mapper.SetmealMapper;

import com.reggie.service.CategoryService;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Transactional
    public void saveWithSetmeal(SetmealDto setmealDto) {
        //保存套餐在setmeal表中
        this.save(setmealDto);
        //保存套餐中的菜品列表到setmeal_dish表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //更改每一个套餐中菜品的 套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());//继承的setmeal的id 就是套餐的id
            setmealDishService.save(setmealDish);
        }
    }

    /**
     * 删除套餐 先删了套餐菜品 再删了套餐
     * @param ids
     */
    @Transactional
    public void removeWithSetmeal(List<Long> ids) {
        //先查套餐菜品有没有ids
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId,ids);
        //不用排序 是用来删的 不是用来看的
        //lqw.orderByAsc(SetmealDish::getSort).orderByDesc(SetmealDish::getUpdateTime);

        //有的话就删掉
        List<SetmealDish> list = setmealDishService.list(lqw);
        if(list != null){
            setmealDishService.remove(lqw);
        }

        setmealService.removeByIds(ids);

    }

    /**
     * 套餐查找回显
     * @param id
     */
    public SetmealDto selelctWithSetmealDto(Long id) {
        //查找基本套餐
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //菜品集合放进去
        //才setmeal_dish里面查找setmeal_id
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(setmealDishList);

        //分类
        //在setmeal表中获取categoryId
        Long categoryId = setmeal.getCategoryId();
        LambdaQueryWrapper<Category> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Category::getId,categoryId);
        String categoryName = categoryService.getOne(lqw2).getName();

        setmealDto.setCategoryName(categoryName);

        return setmealDto;
    }

    /**
     * 套餐修改保存
     * @param setmealDto
     */
    @Transactional
    public void modifyandSave(SetmealDto setmealDto) {
        //存入setmealdish
        //这是一个更新 其实
        //首先通过套餐id查到套餐表的 套餐 再查到套餐菜品表的 菜品
        this.updateById(setmealDto);
        //已经更新套餐
        //更新setmealdish
        //先把原来的删了
        Long setmealId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lqw =new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(lqw);

        //存入现在的setmealDish
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //传过来的数据里面没有setmealid
        //id原本是放在setmealDto的id的
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishService.saveBatch(setmealDishes);
    }


}
